package com.mall.module.system.controller;

import com.mall.common.model.PageResult;
import com.mall.common.result.Result;
import com.mall.module.system.domain.dto.CreateDeptDTO;
import com.mall.module.system.domain.dto.UpdateDeptDTO;
import com.mall.module.system.domain.entity.SysDept;
import com.mall.module.system.domain.vo.CreateDeptVO;
import com.mall.module.system.domain.vo.DeptDetailVO;
import com.mall.module.system.domain.vo.DeptTreeNodeVO;
import com.mall.module.system.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门管理控制器
 */
@Api(tags = "系统部门管理接口")
@RestController
@RequestMapping("/api/dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService deptService;

    @PostMapping("/create")
    @ApiOperation("创建新部门")
    public Result<CreateDeptVO> createDept(@Valid @RequestBody CreateDeptDTO dto) {
        CreateDeptVO result = deptService.createDept(dto);
        return Result.success(result);
    }

    @PutMapping("/update")
    @ApiOperation("更新部门信息")
    public Result<CreateDeptVO> updateDept(@Valid @RequestBody UpdateDeptDTO dto) {
        CreateDeptVO result = deptService.updateDept(dto);
        return Result.success(result);
    }

    @DeleteMapping("/{deptId}")
    @ApiOperation("删除部门")
    public Result<String> deleteDept(@ApiParam(value = "部门ID", required = true) @PathVariable Long deptId) {
        deptService.deleteDept(deptId);
        return Result.success("部门删除成功");
    }

    @GetMapping("/{deptId}")
    @ApiOperation("获取部门详情")
    public Result<DeptDetailVO> getDeptDetail(@ApiParam(value = "部门ID", required = true) @PathVariable Long deptId) {
        DeptDetailVO result = deptService.getDeptDetail(deptId);
        return Result.success(result);
    }

    @GetMapping("/tree")
    @ApiOperation("获取部门树形结构")
    public Result<List<DeptTreeNodeVO>> getDeptTree() {
        List<DeptTreeNodeVO> result = deptService.getDeptTree();
        return Result.success(result);
    }

    @GetMapping("/list")
    @ApiOperation("获取部门列表")
    public Result<List<SysDept>> getDeptList(SysDept dept) {
        List<SysDept> result = deptService.getDeptList(dept);
        return Result.success(result);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询部门列表")
    public Result<PageResult<SysDept>> getDeptPage(
        SysDept dept,
        @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam(value = "页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<SysDept> result = deptService.getDeptPage(dept, pageNum, pageSize);
        return Result.success(result);
    }

    @PutMapping("/{deptId}/status")
    @ApiOperation("更新部门状态")
    public Result<String> updateDeptStatus(
        @ApiParam(value = "部门ID", required = true) @PathVariable Long deptId,
        @ApiParam(value = "状态", required = true) @RequestParam Integer status) {
        deptService.updateDeptStatus(deptId, status);
        return Result.success("部门状态更新成功");
    }

    @PutMapping("/{deptId}/order")
    @ApiOperation("更新部门排序")
    public Result<String> updateDeptOrder(
        @ApiParam(value = "部门ID", required = true) @PathVariable Long deptId,
        @ApiParam(value = "排序号", required = true) @RequestParam Integer orderNum) {
        deptService.updateDeptOrder(deptId, orderNum);
        return Result.success("部门排序更新成功");
    }

    @PutMapping("/order/batch")
    @ApiOperation("批量更新部门排序")
    public Result<String> batchUpdateDeptOrder(@RequestBody List<SysDept> deptOrders) {
        deptService.batchUpdateDeptOrder(deptOrders);
        return Result.success("批量更新部门排序成功");
    }

    @GetMapping("/check/name")
    @ApiOperation("检查部门名称是否存在")
    public Result<Boolean> checkDeptName(
        @ApiParam(value = "部门名称", required = true) @RequestParam String name,
        @ApiParam(value = "父部门ID", required = true) @RequestParam Long parentId,
        @ApiParam(value = "排除的部门ID") @RequestParam(required = false) Long excludeId) {
        boolean exists = deptService.existsByName(name, parentId, excludeId);
        return Result.success(exists);
    }

    @GetMapping("/check/code")
    @ApiOperation("检查部门编码是否存在")
    public Result<Boolean> checkDeptCode(
        @ApiParam(value = "部门编码", required = true) @RequestParam String deptCode,
        @ApiParam(value = "排除的部门ID") @RequestParam(required = false) Long excludeId) {
        boolean exists = deptService.existsByDeptCode(deptCode, excludeId);
        return Result.success(exists);
    }

    @GetMapping("/{deptId}/children")
    @ApiOperation("获取部门的所有子部门ID")
    public Result<List<Long>> getChildDeptIds(@ApiParam(value = "部门ID", required = true) @PathVariable Long deptId) {
        List<Long> result = deptService.getChildDeptIds(deptId);
        return Result.success(result);
    }

    @GetMapping("/{deptId}/path")
    @ApiOperation("获取部门的层级路径")
    public Result<String> getDeptPath(@ApiParam(value = "部门ID", required = true) @PathVariable Long deptId) {
        String result = deptService.getDeptPath(deptId);
        return Result.success(result);
    }

    @GetMapping("/{deptId}/users/count")
    @ApiOperation("统计部门下的用户数量")
    public Result<Integer> countUsersByDeptId(@ApiParam(value = "部门ID", required = true) @PathVariable Long deptId) {
        int result = deptService.countUsersByDeptId(deptId);
        return Result.success(result);
    }

    @PutMapping("/{deptId}/move")
    @ApiOperation("移动部门")
    public Result<String> moveDept(
        @ApiParam(value = "部门ID", required = true) @PathVariable Long deptId,
        @ApiParam(value = "新父部门ID", required = true) @RequestParam Long newParentId) {
        deptService.moveDept(deptId, newParentId);
        return Result.success("部门移动成功");
    }
}
